﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace XNAGameEngine.GameState
{
    /// <summary>
    /// abstract class that may be inherited to define a type of screen transition
    /// </summary>
    public abstract class GameStateTransition
    {
        #region local fields

        //the graphics device used to render to the screen
        protected GraphicsDevice _graphicsDevice;

        //the spritebatch to used to draw the transition images
        protected SpriteBatch _spriteBatch;

        //Get or set the completeness of the transition
        protected float _alpha;

        //is the transition still ACTIVE
        public bool _done;

        //the string string value that this transition corresponds
        //to. This value needs to be set in the constructor.
        public string _transitionName { get; protected set; }

        #endregion

        #region constructors

        /// <summary>
        /// Constructor to setup the transition. Sets _alpha to 0 and _transitionThreshold to 1
        /// </summary>
        /// <param name="graphicsDevice">the graphics device from the GameEngine</param>
        /// <param name="spriteBatch">the spritebatch from the GameEngine</param>5
        public GameStateTransition(GraphicsDevice graphicsDevice, SpriteBatch spriteBatch)
        {
            _graphicsDevice = graphicsDevice;
            _spriteBatch = spriteBatch;
            _alpha = 0;
        }

        #endregion

        #region local methods

        /// <summary>
        /// Method to call when the GameState is being transitions in from not visible to visible
        /// </summary>
        /// <returns>true when the transition is complete, false otherwise</returns>
        public virtual bool TransitionIn(GameTime gameTime)
        {
            Draw();

            if (_alpha > 0)
            {
                _alpha -= ((float)(gameTime.ElapsedRealTime.Milliseconds)) / 500;
                return false;
            }

            _alpha = 0;
            return true;
        }

        /// <summary>
        /// Method to call when the GameState is being transitions out from visible to not visible
        /// </summary>
        /// <returns>true when the transition is complete, false otherwise</returns>
        public virtual bool TransitionOut(GameTime gameTime)
        {
            Draw();

            if (_alpha < 1)
            {
                _alpha += ((float)(gameTime.ElapsedRealTime.Milliseconds)) / 500;
                return false;
            }

            _alpha = 1;
            return true;
        }

        /// <summary>
        /// Protected method to do the drawing of the transition.
        /// Protected because it is called from within TransitionIn() or TransitionOut().
        /// </summary>
        protected abstract void Draw();

        #endregion
    }
}